<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Encore\Admin\Facades\Admin;
use App\Models\App;

class AppController extends NoidController
{
	public $title = '应用管理';

	
    /**
     * Get content title.
     *
     * @return string
     */
    public function title()
    {
        return $this->title;
    }



    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {  
		$grid = new Grid(new App());
		
		$grid->model()->orderBy('item_name','asc')->orderBy('created_at','asc');
		$grid ->disablePagination();


		$grid->tools->disableBatchActions(true);

		$grid->column('name', '名称');
		$grid->column('title', '标题');
		$grid->column('item_name', '组名');
		$grid->column('table', '表名');
		$grid->column('_manager', '管理')->display(function () {
					$re = '';
					$opens = $this->sets['base'] ?? [];
					foreach(config('app.appset') as $name=>$app){
						if(!empty($app['manager']) && !empty($opens[$name])) {
							$re.="<a href='".admin_base_path('apps/'.$this->name.'/'.$name)."'>".$app['title']."管理</a><br />";
						}
					}
					return $re;
				});
		$grid->column('_set', '设置')->display(function (){
					return "<a href='".route('admin.app.set',['name'=>$this->name])."')'>设置</a>";
				});		
			
        return $grid;
    }

   

	/**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
			

		$tables = config('app.table');
		$tablesel = empty($tables) ? [] : array_keys($tables);
		
		$tablesel = array_combine($tablesel,$tablesel);
		$form = new Form(new App());
		$form->text('name', '名称')
			->creationRules('required|unique:apps,name|regex:/^[a-z][a-z_]+$/',['regex' => '名称必须是英文开头且只包含小写字母、下划线'])
			->updateRules('required|unique:apps,name,{{name}}|regex:/^[a-z][a-z_]+$/',['regex' => '名称必须是英文开头且只包含小写字母、下划线']);
 		$form->text('title', '标题')->help('简单描述应用')->rules('required');
		$form->text('item_name', '组名')->help('应用组是由若干个应用组合而成的复合应用');
		$form->select('table', '表名')
			->options($tablesel)
			->rules('required');

		$form->embeds('ctrl','控制器设置', function ($form) {
			$form->text('admin','后台')->help('一般不填,默认根据表名在config/app.php中设置');
			$form->text('web','前台')->help('一般不填,默认根据表名在config/app.php中设置');
			$form->text('api','Api')->help('一般不填,默认根据表名在config/app.php中设置');
		});
        return $form;

    }

    public function show($name, Content $content)
    {
		return $content->header('应用记录')
			->breadcrumb(
					['text' => '应用管理', 'url' => 'app'],
					['text' => '应用记录']
				)
            ->description('详情')
            ->body(Admin::show(App::findOrFail($name), function (Show $show) {
				$show->name('名称');
                $show->title('标题');
                $show->table('表');
				$show->sets('设置')->unescape()->as(function ($sets) {
						if(empty($sets)) return '';
						$re = '';
						foreach($sets as $name => $v ){
							$re .= $name.'设置:<pre><code>'.json_encode($v,JSON_UNESCAPED_UNICODE).'</code></pre>'; 
						}
						return $re;
					});
        		}));
    }



}
